package com.tensquare.base.service;

import com.tensquare.base.dao.LableDao;
import com.tensquare.base.doman.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class LabelService {

    //如果涉及到增删改,需要开启事务管理

    @Autowired
    private LableDao lableDao;
    @Autowired
    private IdWorker idWorker;

    @Transactional   //开启事务
    public void save(Label label) {
        label.setId(idWorker.nextId()+"");
        lableDao.save(label);
        //int i = 1/0;
    }

    public Label findOne(String labelIdx) {
     return lableDao.findById(labelIdx).get();
    }

    public List<Label> findAll() {
        return lableDao.findAll();
    }
    @Transactional
    public void update(Label label) {
        lableDao.save(label);
    }
    @Transactional
    public void delete(String labelId) {
        lableDao.deleteById(labelId );
       // int i = 1/0;
    }

    /**
     * 条件查询
     * @param map
     * @return
     *  List<T> findAll(@Nullable Specification<T> var1);
     */
    public List<Label> search(Map map) {
        Specification specification = getSpecification(map);
        return lableDao.findAll(specification);

    }
    /**思想:ORM框架,操作实体类就相当于操作实体类中的表
     * 条件分页查询
     *  Page<T> findAll(@Nullable Specification<T> var1, Pageable var2);
     */
    public Specification getSpecification(Map map){
        if (map.isEmpty()){
            return null;
        }
        return new Specification(){

            @Nullable
            @Override
            /**
             * 参数含义
             * root:根实体
             * criteriaQuery:一般表单操作,可以设置sql from,where,group by,order by
             * criteriaBuilder:Predicate实例工厂
             */
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                String labelname = (String) map.get("labelname");
                if (!StringUtils.isEmpty(labelname)){
                    //sql = from label
                    //sql = where labelname like ? (条件一)
                    //参数1:查询实体类中属性,参数2:查询具体zhi
                    Predicate labelname1 = criteriaBuilder.like(root.get("labelname").as(String.class), "%" + labelname + "%");
                    list.add(labelname1);
                }
                String state = (String) map.get("state");
                if (!StringUtils.isEmpty(state )){
                    //sql += where state = ?(条件二)
                    Predicate state1 = criteriaBuilder.equal(root.get("state").as(String.class), state);
                    list.add(state1);
                }
                String recommend = (String) map.get("recommend");
                if (!StringUtils.isEmpty(recommend)){
                    //sql += where recommend = ?(条件三)
                    Predicate recommend1 = criteriaBuilder.equal(root.get("recommend").as(String.class), recommend);
                    list.add(recommend1);
                }
                //criteriaBuilder.and 讲条件使用and拼接
                Predicate[] predicates = new Predicate[list.size()];
                //将集合转为数组
                predicates= list.toArray(predicates);
                return criteriaBuilder.and(predicates);
            }
        };
    }

    public Page search(Map map, Pageable pageable) {
        Specification specification = getSpecification(map);
        return lableDao.findAll(specification,pageable);
    }
}
